#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>

using namespace std;

using ll = long long;
const int MAXN = 300005;
const ll INF = 4e18; 

struct Edge {
    int to;
    int weight;
};

vector<Edge> adj[MAXN];
int n;
ll s;

ll dist[MAXN];
int p[MAXN]; 

int bfs(int start, int N, int& farnds) {
    fill(dist + 1, dist + N + 1, -1);
    fill(p + 1, p + N + 1, 0);
    deque<int> q;

    dist[start] = 0;
    q.push_back(start);

    farnds = start;
    while (!q.empty()) {
        int u = q.front();
        q.pop_front();
        if (dist[u] > dist[farnds]) {
            farnds = u;
        }
        for (const auto& edge : adj[u]) {
            int v = edge.to;
            int w = edge.weight;
            if (dist[v] == -1) {
                dist[v] = dist[u] + w;
                p[v] = u; 
                q.push_back(v);
            }
        }
    }
    return farnds;
}



ll bdist[MAXN];
bool d[MAXN];

void dfs(int u, int p) {
    bdist[u] = 0;
    for (const auto& edge : adj[u]) {
        int v = edge.to;
        int w = edge.weight;
        
        if (v == p || d[v]) {
            continue;
        }
        dfs(v, u);
        
        bdist[u] = max(bdist[u], bdist[v] + w);
    }
}

void solve() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    if (!(cin >> n >> s)) return; 
    for (int i = 0; i < n - 1; ++i) {
        int u, v, w;
        if (!(cin >> u >> v >> w)) return;
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
    }
    if (n == 1) { 
        cout << 0 << endl;
        return;
    }

    int A = 0, B = 0;

    A = bfs(1, n, A); 
    B = bfs(A, n, B); 

    vector<int> dpath;
    int curr = B;
    
    while (curr != 0) {
        dpath.push_back(curr);
        d[curr] = true;
        curr = p[curr];
    }
    reverse(dpath.begin(), dpath.end());
    
    int k = dpath.size();
    vector<ll> d(k); 
    vector<ll> e(k); 
    for (int i = 0; i < k; ++i) {
        d[i] = dist[dpath[i]];
    }
    for (int i = 0; i < k; ++i) {
        dfs(dpath[i], 0);
        e[i] = bdist[dpath[i]];
    }

    vector<ll> L(k + 1, -INF); 
    vector<ll> R(k + 1, -INF); 

    for (int i = 0; i < k; ++i) {
        L[i + 1] = max(L[i], e[i] - d[i]);
    }

    for (int i = k - 1; i >= 0; --i) {
        R[i] = max(R[i + 1], e[i] + d[i]);
    }

    ll ansdist = INF;
    deque<int> dq; 
    int p1 = 0; 
    
    for (int p2 = 0; p2 < k; ++p2) { 
        while (!dq.empty() && e[dq.back()] <= e[p2]) {
            dq.pop_back();
        }
        dq.push_back(p2);
        
        while (d[p2] - d[p1] > s) {
            if (!dq.empty() && dq.front() == p1) {
                dq.pop_front();
            }
            p1++;
        }
        
        ll maxmid = e[dq.front()];
        
        ll maxleft = -INF;
        if (p1 > 0) {
            maxleft = L[p1] + d[p1]; 
        }
        
        ll maxright = -INF;
        if (p2 < k - 1) {
            maxright = R[p2 + 1] - d[p2];
        }

        
        ll curmax = max({maxmid, maxleft, maxright});
        ansdist = min(ansdist, curmax);
    }

    cout << ansdist << "\n";
}

int main() {
    solve();
}